<!-- components/ToastMessage.vue -->
<template>
    <Teleport to="body">
      <!-- 遮罩层（可选） -->
      <div
        v-if="visible || isLeaving"
        class="toast-mask"
        :class="{ 'mask-active': visible && !isLeaving }"
      ></div>
  
      <!-- Toast内容 -->
      <div
        v-if="visible || isLeaving"
        class="toast-wrapper"
        :class="[
          `toast-${position}`,
          { 'toast-active': visible && !isLeaving, 'toast-leave': isLeaving },
        ]"
      >
        <div class="toast-content">
          {{ message }}
        </div>
      </div>
    </Teleport>
  </template>
    
    <script>
  export default {
    name: "ToastMessage",
    props: {
      message: {
        type: String,
        required: true,
      },
      duration: {
        type: Number,
        default: 2000,
      },
      position: {
        type: String,
        default: "center", // top | center | bottom
        validator: (val) => ["top", "center", "bottom"].includes(val),
      },
    },
    data() {
      return {
        visible: false,
        isLeaving: false,
      };
    },
    emits: ["closed"],
    mounted() {
      setTimeout(() => {
        this.visible = true;
        this.autoClose();
      }, 50);
    },
    methods: {
      autoClose() {
        setTimeout(() => {
          this.startLeaveAnimation();
        }, this.duration);
      },
      startLeaveAnimation() {
        this.isLeaving = true;
        setTimeout(() => {
          this.visible = false;
          this.isLeaving = false;
          this.$emit("closed");
        }, 300);
      },
    },
  };
  </script>
    
    <style scoped>
  /* 基础动画 */
  .toast-wrapper {
    position: fixed;
    left: 50%;
    opacity: 0;
    transform: translateX(-50%);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 2000;
    will-change: transform, opacity;
  }
  
  /* 不同位置 */
  .toast-top {
    top: 20px;
    transform: translateX(-50%) translateY(-20px);
  }
  
  .toast-center {
    top: 50%;
    transform: translate(-50%, -50%) translateY(20px);
  }
  
  .toast-bottom {
    bottom: 20px;
    transform: translateX(-50%) translateY(20px);
  }
  
  /* 激活状态 */
  .toast-active {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
  
  .toast-center.toast-active {
    transform: translate(-50%, -50%) translateY(0);
  }
  
  /* 离开动画 */
  .toast-leave {
    opacity: 0;
    transform: translateX(-50%) translateY(-20px);
    transition: all 0.2s ease-in;
  }
  
  .toast-center.toast-leave {
    transform: translate(-50%, -50%) translateY(20px);
  }
  
  .toast-bottom.toast-leave {
    transform: translateX(-50%) translateY(20px);
  }
  
  /* 内容样式 */
  .toast-content {
    padding: 22px 40px;
    background: var(--bs-bg-color);
    color: var(--bs-link-color);
    border-radius: 4px;
    font-size: 14px;
    line-height: 1.5;
    max-width: 70vw;
    word-break: break-word;
    box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;
    border: var(--bs-theme-color) 2px solid;
  }
  
  /* 遮罩层（如果需要带遮罩的toast） */
  .toast-mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.3);
    backdrop-filter: blur(2px);
    transition: opacity 0.3s;
    z-index: 1999;
  }
  
  .toast-mask.mask-active {
    background-color: rgba(255,255,255,.3);
    opacity: 1;
  }
  </style>